﻿using OABackground.Entities;
using OABackground.Infrastructure;
using System;
using System.Collections.Generic;
using System.Text;

namespace OABackground.Services
{
    public class WageService
    {
        readonly WageRepository wageRepository = new WageRepository();
        public void AddWage(string teacherid, string wagetime, float basicwage, float overtimewage, float welfare, float bonus)
        {
            wageRepository.Add(teacherid, wagetime, basicwage, overtimewage, welfare, bonus);
        }
        public void DeleteWage(int id)
        {
            wageRepository.Delete(id);
        }
        public Wage QuerybyID(int id)
        {
            return wageRepository.QuerybyID(id);
        }
        public List<Wage> QueryTeacherWage(string teacherid, string year, string month, string yearmonth)
        {
            List<Wage> wlist = new List<Wage>();
            if (string.IsNullOrEmpty(teacherid) && string.IsNullOrEmpty(year) && string.IsNullOrEmpty(yearmonth))   //全空,查所有
            {
                return wageRepository.QueryTAll();
            }
            if (!string.IsNullOrEmpty(teacherid) && string.IsNullOrEmpty(year) && string.IsNullOrEmpty(month)&& string.IsNullOrEmpty(yearmonth))   //只有工号
            {
                return wageRepository.QuerybyTid(teacherid);
            }
            if (!string.IsNullOrEmpty(teacherid) && string.IsNullOrEmpty(year) && string.IsNullOrEmpty(month) && !string.IsNullOrEmpty(yearmonth))   //只有工号
            {
                var a = wageRepository.QuerybyTidMonth(teacherid, yearmonth);
                if (!string.IsNullOrEmpty(a.Teacherid)) wlist.Add(a);
                return wlist;
            }
            if (string.IsNullOrEmpty(year) && !string.IsNullOrEmpty(month))   //工号+月
            {
                return wageRepository.QuerybyTidAllMonth(teacherid, month);
            }
            if (!year.Equals(yearmonth)) //year!=yearmonth，说明有传进来了月数参数，按某年月查询某一月工资    工号+年+月
            {
                var a = wageRepository.QuerybyTidMonth(teacherid, yearmonth);
                if (!string.IsNullOrEmpty(a.Teacherid)) wlist.Add(a);
                return wlist;
            }
            return wageRepository.QueryAllbyTidYear(teacherid, year);     //工号+年
        }
        public List<Wage> QueryDepartmentWage(string departmentid, string time)
        {
            if (string.IsNullOrEmpty(time)) //时间为空，按部门查
            {
                return wageRepository.QueryAllbyDid(departmentid);
            }
            return wageRepository.QueryAllbyDidMonth(departmentid, time);
        }
        public Object QueryDepartmentWage2(string departmentid, string time)
        {
            if (string.IsNullOrEmpty(time)) //时间为空，按部门查
            {
                return wageRepository.QueryAllbyDid2(departmentid);
            }
            return wageRepository.QueryAllbyDidMonth2(departmentid, time);
        }
        public void UpdateWage(int id, float basicwage, float overtimewage, float welfare, float bonus)
        {
            var wage = wageRepository.QuerybyID(id);
            wage.Basicwage = basicwage;
            wage.Overtimewage = overtimewage;
            wage.Welfare = welfare;
            wage.Bonus = bonus;
            wage.Totalwage = basicwage + overtimewage + welfare + bonus;
            wageRepository.Update(wage);
        }

    }
}
